import pygame
import speech

laser = pygame.mixer.Sound("sounds/laser.ogg")
lockOn = pygame.mixer.Sound("sounds/lock.ogg")
incoming = pygame.mixer.Sound("sounds/incoming.ogg")
explosion = pygame.mixer.Sound("sounds/explode.ogg")


def gameAction(player, aliens):
    #variable section
    lpan = 1.0 # Left channel set to max
    rpan = 0.0 #right channel set to minimum
    x = player.getxpos()
    trackShot = False
    if player.getFireState() == True:
        #track the laser shot that is fired.
        trackShot = True
        #set volume on left and right channels based on position of the ship
        i = 0
        while i < x:
            if lpan > 0.0: lpan -= 0.01
            if rpan < 1.0: rpan += 0.01
            i += 1
        if player.getLastFire() == True:
            laserChannel = laser.play()
            if laserChannel is not None: laserChannel.set_volume(lpan, rpan)
    #update the ship's position
    player.move()
    #Update the aliens positions and fire their lasers
    alienIndex = 0
    for i in aliens:
        if i.getxpos() == x:
            if i.laserFire() == 1: incomingChannel = incoming.play()
            if i.laserFire() == 2: player.hit(10)
        i.move()
        if i.getypos() <= 0: player.hit(20)
        #check if alians are targeted or shot down.
        if i.getxpos() == x:
            lockOn.play()
        if i.getxpos() == x and player.getFireState() == True:
            if trackShot == True:
                explodeChannel = explosion.play()
                player.setScore(10)
                del aliens[alienIndex]
        alienIndex += 1
    return True

def tweet(username,pswd,gameScore):
    import urllib
    data = urllib.urlencode({"status" : "I just scored " + str(gameScore) + " points in Dark-invaders."})
    result = urllib.urlopen("http://%s:%s@twitter.com/statuses/update.xml" % (username,pswd), data)

def getInput(keyEcho = True):
    text = ""
    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN:
            if event.key != pygame.K_BACKSPACE:
                text += event.unicode
                if keyEcho == True: speech.speak(event.unicode)
            if event.key == pygame.K_BACKSPACE:
                if keyEcho == True: speech.speak(text[len(text) - 1])
                text = text[: - 1]
#                text = str(tempString)
            if event.key == pygame.K_RETURN: break
            if event.key == pygame.K_ESCAPE:
                text = ""
                break
    return text

def getChoice():
    import commands
    answer = True
    while True:
        event = pygame.event.wait()
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                if answer == True:
                    answer = False
                else:
                    answer = True
            if event.key == pygame.K_y: answer = True
            if event.key == pygame.K_n: answer = False
            if answer == True: speech.speak("Yes")
            if answer == False: speech.speak("No")
            if event.key == pygame.K_RETURN: break
    return answer

def writeVar(fileName, sectionName, sectionKey, sectionValue):
    import ConfigParser
    import os
    config = ConfigParser.ConfigParser()
    if os.path.exists(fileName):
        config.read(fileName)
    try:
        config.set(sectionName, sectionKey, sectionValue)
    except:
        config.add_section(sectionName)
        config.set(sectionName, sectionKey, sectionValue)
    with open(fileName, "wb") as configFile:
        config.write(configFile)
        configFile.close()

def getVar(fileName, sectionName, sectionKey):
    import ConfigParser
    import os
    config = ConfigParser.ConfigParser()
    if os.path.exists(fileName) == True:
        try:
            config.read(fileName)
            return config.get(sectionName, sectionKey)
        except:
            return ""
    else:
        return ""

def getTwitter():
    cFile = ".config"
    msg = "Please enter your Twitter user name."
    speak(msg)
    twitterName = getInput()
    msg = "Please enter your Twitter password."
    speak(msg)
    twitterPassword = getInput()
    writeVar(cFile, "twitter", "username", twitterName)
    writeVar(cFile, "twitter", "password", twitterPassword)

